﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace KalevaAalto
{
    public static partial class Static
    {
        public static string NowNumberString => DateTime.Now.ToDateTimeNumberString();


        public const string LowerLetters = @"abcdefghijklmnopqrstuvwxyz";

        public static string TestFileName => $"Test_{NowNumberString}_{GetRandomString(LowerLetters, 10)}";


        public static string ToDateTimeNumberString(this DateTime dateTime)=>dateTime.ToString(@"yyyyMMddHHmmss");

        public static string ToStandradDateString(this DateTime date) => date.ToString(@"yyyy-MM-dd");



        /// <summary>
        /// 获取某个日期所在月份的第一天
        /// </summary>
        /// <param name="date">要解析的日期</param>
        /// <returns>返回日期date日期所在月份的第一天</returns>
        public static DateTime MonthFirstDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1);
        }

        /// <summary>
        /// 获取某个日期所在月份的最后一天
        /// </summary>
        /// <param name="date">要解析的日期</param>
        /// <returns>返回日期date日期所在月份的最后一天</returns>
        public static DateTime MonthLastDay(this DateTime date)
        {
            return date.AddMonths(1).MonthFirstDay().AddDays(-1);
        }

        /// <summary>
        /// 获取某个日期所在年份的第一天
        /// </summary>
        /// <param name="date">要解析的日期</param>
        /// <returns>返回日期date日期所在年份的第一天</returns>
        public static DateTime YearFirstDay(this DateTime date)
        {
            return new DateTime(date.Year, 1, 1);
        }

        /// <summary>
        /// 获取某个日期所在年份的最后一天
        /// </summary>
        /// <param name="date">要解析的日期</param>
        /// <returns>返回日期date日期所在年份的最后一天</returns>
        public static DateTime YearLastDay(this DateTime date)
        {
            return new DateTime(date.Year, 12, 31);
        }


        public static string ToNumberString(this DateTime date)
        {
            return date.ToString(@"yyyyMMddhhmmss");
        }


        private static readonly Regex s_stringToMonthRegex = new Regex(@"(?<year>\d{4})(?<month>\d{2})");
        public static DateTime ToMonth(this string monthString)
        {
            Match match = s_stringToMonthRegex.Match(monthString);

            if (!match.Success)
            {
                return DateTime.MinValue;
            }

            int year = Convert.ToInt32(match.Groups[@"year"].Value).Between(DateTime.MinValue.Year, DateTime.MaxValue.Year);
            int month = Convert.ToInt32(match.Groups[@"month"].Value).Between(1, 12);


            return new DateTime(year,month,1);


        }

    }
}
